package com.wdc.贪心;

import java.security.Key;
import java.util.Arrays;
import java.util.stream.IntStream;

@SuppressWarnings("all")
public class E07Leetcode1005 {
    public int largestSumAfterKNegations(int[] nums, int k) {
         nums = IntStream.of(nums)
                .boxed()
                .sorted((o1, o2) -> Math.abs(o2) - Math.abs(o1))
                .mapToInt(Integer::intValue).toArray();

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < 0 && k > 0) {
                nums[i] = -nums[i];
                k--;
            }
        }

        if (k % 2 == 1) nums[nums.length - 1] = -nums[nums.length - 1];
        return Arrays.stream(nums).sum();
    }
}
